/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.query;

import nfse.gui.client.NetFSEClient;
import nfse.gui.client.QueryService;
import nfse.gui.client.QueryServiceAsync; 
import com.google.gwt.user.datepicker.client.*;

import nfse.gui.util.GUIUtil;
import nfse.gui.util.NetUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.ToolTip;

import java.util.Date;
import java.util.Vector;

public class SearchTab extends FlexTable {

	final RadioButton asDstRadioButton = new RadioButton("SearchTypeGroup",
			"as Dst");

	final RadioButton asSrcRadioButton = new RadioButton("SearchTypeGroup",
			"as Src");

	private CheckBox[] checkBoxes = new CheckBox[0];

	private final FlexTable checkBoxTable = new FlexTable();

	NetFSEClient client = null;

	final TextBox dstip1TextBox = new TextBox();

	final TextBox dstip2TextBox = new TextBox();

	final ListBox dstipPredComboBox = new ListBox();

	final TextBox dstport1TextBox = new TextBox();

	final TextBox dstport2TextBox = new TextBox();

	final ListBox dstportPredComboBox = new ListBox();

	final DateBox endDatePicker = new DateBox();

	final RadioButton hexPayloadRadioButton = new RadioButton(
			"PayloadTypeGroup", "Hex");

	final TextArea ips = new TextArea();

	final RadioButton ipsRadioButton = new RadioButton("SearchGroup",
			"IP addresses and/or subnets");

	final Label label = new Label();

	final TextBox limitTextBox = new TextBox();

	final TextArea noteTextArea = new TextArea();

	final RadioButton payloadRadioButton = new RadioButton("SearchGroup",
			"Payload");

	final Button queryButton = new Button("Search");

	final RadioButton regExPayloadRadioButton = new RadioButton(
			"PayloadTypeGroup", "RegEx");

	final Button resetButton = new Button("Reset");

	final RadioButton searchCriteriaRadioButton = new RadioButton(
			"SearchGroup", "Search criteria");

	final FlexTable searchCriteriaTable = new FlexTable();

	final RadioButton srcAndDstRadioButton = new RadioButton("SearchTypeGroup",
			"Src and Dst");

	final TextBox srcip1TextBox = new TextBox();

	final TextBox srcip2TextBox = new TextBox();

	final ListBox srcipPredComboBox = new ListBox();

	final DateBox startDatePicker = new DateBox();

	final RadioButton textPayloadRadioButton = new RadioButton(
			"PayloadTypeGroup", "Text");

	private int[] typeIdxs = new int[0];

	private String[] typeNames = new String[0];

	private int[] types = new int[0];

	final CheckBox unlimitedCheckBox = new CheckBox("Unlimited");

	public SearchTab(NetFSEClient tempClient) {
		this.client = tempClient;

		srcipPredComboBox.setVisibleItemCount(1);
		srcipPredComboBox.clear();
		srcipPredComboBox.addItem("=");
		srcipPredComboBox.addItem("between");

		dstipPredComboBox.setVisibleItemCount(1);
		dstipPredComboBox.clear();
		dstipPredComboBox.addItem("=");
		dstipPredComboBox.addItem("between");

		dstportPredComboBox.setVisibleItemCount(1);
		dstportPredComboBox.clear();
		dstportPredComboBox.addItem("=");
		dstportPredComboBox.addItem("between");

		textPayloadRadioButton.setValue(true);

		srcipPredComboBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent sender) {
				if (srcipPredComboBox.getSelectedIndex() == 0) {
					searchCriteriaTable.getWidget(0, 3).setVisible(false);
					searchCriteriaTable.getWidget(0, 4).setVisible(false);
				} else {
					searchCriteriaTable.getWidget(0, 3).setVisible(true);
					searchCriteriaTable.getWidget(0, 4).setVisible(true);
				}
			}
		});

		dstipPredComboBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent sender) {
				if (dstipPredComboBox.getSelectedIndex() == 0) {
					searchCriteriaTable.getWidget(1, 3).setVisible(false);
					searchCriteriaTable.getWidget(1, 4).setVisible(false);
				} else {
					searchCriteriaTable.getWidget(1, 3).setVisible(true);
					searchCriteriaTable.getWidget(1, 4).setVisible(true);
				}
			}
		});

		dstportPredComboBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent sender) {
				if (dstportPredComboBox.getSelectedIndex() == 0) {
					searchCriteriaTable.getWidget(2, 3).setVisible(false);
					searchCriteriaTable.getWidget(2, 4).setVisible(false);
				} else {
					searchCriteriaTable.getWidget(2, 3).setVisible(true);
					searchCriteriaTable.getWidget(2, 4).setVisible(true);
				}
			}
		});

		unlimitedCheckBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				if (unlimitedCheckBox.getValue()) {
					limitTextBox.setEnabled(false);
				} else {
					limitTextBox.setEnabled(true);
				}
			}
		});

		ips.setVisibleLines(5);

		FlexTable leftQueryTable = new FlexTable();
		FlexTable.FlexCellFormatter formatter = leftQueryTable
				.getFlexCellFormatter();
		int idx = -1;

		leftQueryTable.setHTML(++idx, 0, "<h3>Date and Time</h3>");
		formatter.setColSpan(idx, 0, 2);

		leftQueryTable.setHTML(++idx, 0, "Start");
		formatter.addStyleName(idx, 0, "tdright");
		leftQueryTable.setWidget(idx, 1, startDatePicker);
		leftQueryTable.setHTML(++idx, 0, "End");
		formatter.addStyleName(idx, 0, "tdright");
		leftQueryTable.setWidget(idx, 1, endDatePicker);

		leftQueryTable.setHTML(++idx, 0, "<h3>Types</h3>");
		formatter.setColSpan(idx, 0, 2);

		leftQueryTable.setWidget(++idx, 0, checkBoxTable);
		formatter.setColSpan(idx, 0, 2);

		// formatter.setColSpan(idx, 0, 2);

		leftQueryTable.setHTML(++idx, 0, "<b>Note</b>");
		formatter.setColSpan(idx, 0, 2);
		noteTextArea.setVisibleLines(5);
		leftQueryTable.setWidget(++idx, 0, noteTextArea);
		formatter.setColSpan(idx, 0, 2);

		idx = -1;
		FlexTable rightQueryTable = new FlexTable();
		formatter = rightQueryTable.getFlexCellFormatter();

		rightQueryTable.setHTML(++idx, 0, "<h3>Search Information</h3>");

		rightQueryTable.setWidget(++idx, 0, ipsRadioButton);

		FlexTable ipsFlexTable = new FlexTable();

		ipsFlexTable.setHTML(0, 0, "IP List");
		ipsFlexTable.setHTML(0, 1, "Search");
		FlexTable searchTypeTable = new FlexTable();
		searchTypeTable.setWidget(0, 0, srcAndDstRadioButton);
		searchTypeTable.setWidget(1, 0, asSrcRadioButton);
		searchTypeTable.setWidget(2, 0, asDstRadioButton);
		ipsFlexTable.setWidget(1, 0, ips);
		ipsFlexTable.setWidget(1, 1, searchTypeTable);

		rightQueryTable.setWidget(++idx, 0, ipsFlexTable);

		rightQueryTable.setWidget(++idx, 0, searchCriteriaRadioButton);

		FlexTable.FlexCellFormatter searchCriteriaFormatter = searchCriteriaTable
				.getFlexCellFormatter();

		searchCriteriaTable.setHTML(0, 0, "Source IP");
		searchCriteriaFormatter.addStyleName(0, 0, "tdright");
		searchCriteriaFormatter.setWordWrap(0, 0, false);
		searchCriteriaTable.setWidget(0, 1, srcipPredComboBox);
		searchCriteriaTable.setWidget(0, 2, srcip1TextBox);
		searchCriteriaTable.setWidget(0, 3, new Label("and"));
		searchCriteriaTable.setWidget(0, 4, srcip2TextBox);

		searchCriteriaTable.setHTML(1, 0, "Destination IP");
		searchCriteriaFormatter.addStyleName(1, 0, "tdright");
		searchCriteriaFormatter.setWordWrap(1, 0, false);
		searchCriteriaTable.setWidget(1, 1, dstipPredComboBox);
		searchCriteriaTable.setWidget(1, 2, dstip1TextBox);
		searchCriteriaTable.setWidget(1, 3, new Label("and"));
		searchCriteriaTable.setWidget(1, 4, dstip2TextBox);

		searchCriteriaTable.setHTML(2, 0, "Destination Port");
		searchCriteriaFormatter.setWordWrap(2, 0, false);
		searchCriteriaFormatter.addStyleName(2, 0, "tdright");
		searchCriteriaTable.setWidget(2, 1, dstportPredComboBox);
		searchCriteriaTable.setWidget(2, 2, dstport1TextBox);
		searchCriteriaTable.setWidget(2, 3, new Label("and"));
		searchCriteriaTable.setWidget(2, 4, dstport2TextBox);

		rightQueryTable.setWidget(++idx, 0, searchCriteriaTable);

		payloadRadioButton.setEnabled(false);
		// rightQueryTable.setWidget(++idx, 0, payloadRadioButton);

		// FlexTable payloadTable = new FlexTable();

		// payloadTable.setWidget(0, 0, payloadTextBox);
		// FlexTable payloadOptionsTable = new FlexTable();
		// textPayloadRadioButton.setEnabled(false);
		// payloadOptionsTable.setWidget(0, 0, textPayloadRadioButton);
		// hexPayloadRadioButton.setEnabled(false);
		// payloadOptionsTable.setWidget(1, 0, hexPayloadRadioButton);
		// regExPayloadRadioButton.setEnabled(false);
		// payloadOptionsTable.setWidget(2, 0, regExPayloadRadioButton);

		// payloadTable.setWidget(0, 1, payloadOptionsTable);

		// rightQueryTable.setWidget(++idx, 0, payloadTable);

		rightQueryTable.setHTML(++idx, 0, "<b>Per-Type Record Limit</b>");
		formatter.setColSpan(idx, 0, 2);
		rightQueryTable.setWidget(++idx, 0, unlimitedCheckBox);
		formatter.setColSpan(idx, 0, 2);
		rightQueryTable.setWidget(++idx, 0, limitTextBox);
		formatter.setColSpan(idx, 0, 2);

		this.setWidget(0, 0, leftQueryTable);
		this.setWidget(0, 1, rightQueryTable);

		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(queryButton);
		buttonPanel.add(resetButton);

		this.setWidget(1, 0, buttonPanel);

		queryButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {

				if (!unlimitedCheckBox.getValue()) {
					if (limitTextBox.getText().length() > 0) {
						try {
							int limit = Integer
									.parseInt(limitTextBox.getText());
							if (limit <= 0) {
								MessageBox
										.alert("The value specified in the limit field must be a valid integer greater than 0.");
								return;
							}
						} catch (Exception e) {
							MessageBox
									.alert("The value specified in the limit field must be a valid integer greater than 0.");
							return;
						}
					}
				}

				boolean start = (startDatePicker.getValue() != null);
				if (!start
						&& startDatePicker.getTextBox().getText().length() == 0) {
					MessageBox.alert("You must enter a start date and time.");
					return;

				} else if (!start) {
					MessageBox
							.alert("The start timestamp specified was invalid: '"
									+ startDatePicker.getTextBox().getText()
									+ "'");
					return;
				}
				
				boolean end = (endDatePicker.getValue() != null);
				if (!end
						&& endDatePicker.getTextBox().getText().length() == 0) {
					MessageBox.alert("You must enter an end date and time.");
					return;

				} else if (!end) {
					MessageBox
							.alert("The end timestamp specified was invalid: '"
									+ endDatePicker.getTextBox().getText()
									+ "'");
					return;
				}

				int[] types = client.getSelectedTypes();
				if (types.length == 0) {
					MessageBox
							.alert("At least one type must be selected in the 'Types' section.");
					return;
				}

				if (!ipsRadioButton.getValue()
						&& !searchCriteriaRadioButton.getValue()) {
					MessageBox
							.alert("You must select either 'IP addresses and/or subnets' or 'Search criteria' from the 'Search Information' section.");
					return;
				}

				if (ipsRadioButton.getValue()) {
					if (ips.getText().trim().length() == 0) {
						MessageBox
								.alert("You must enter at least one IP address or subnet to search on.");
						return;
					}
				}
				if (searchCriteriaRadioButton.getValue()) {
					if (srcip1TextBox.getText().trim().length() == 0
							&& dstip1TextBox.getText().trim().length() == 0
							&& dstport1TextBox.getText().trim().length() == 0) {
						MessageBox
								.alert("You must enter a value in at least one of the 'Destination IP', 'Source IP' or 'Destination Port' fields.");
						return;
					}
				}
				// Window.alert("Search in progress. Your results will return
				// shortly.");
				query();
			}
		});

		resetButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				initWidgets();
			}
		});

		initWidgets();

	}

	private String generateQuery() {

		String query = "<Query>";
		// String query = "<Query >";
		if (ipsRadioButton.getValue()) {
			String ips = this.ips.getText().replaceAll("\n", " ");

			String[] ipArray = ips.split(" +");

			for (int i = 0; i < ipArray.length; i++) {
				ipArray[i] = ipArray[i].trim();
				String[] ips2 = ipArray[i].split("\\.");

				if (ips2.length == 4) {
					if ((asSrcRadioButton.getValue())
							|| (srcAndDstRadioButton.getValue()))
						query += "<Query srcip=\"" + ipArray[i] + "\"/>";
					if ((asDstRadioButton.getValue())
							|| (srcAndDstRadioButton.getValue()))
						query += "<Query dstip=\"" + ipArray[i] + "\"/>";
				} else if (ips2.length < 4) {
					String ip1 = "";
					String ip2 = "";
					for (int x = 0; x < ips2.length; x++) {
						if (x > 0) {
							ip1 += ".";
							ip2 += ".";
						}
						ip1 += ips2[x];
						ip2 += ips2[x];
					}
					for (int x = 0; x < 4 - ips2.length; x++) {
						ip1 += ".0";
						ip2 += ".255";
					}
					// alert(ip1 + ", " + ip2);
					if ((asSrcRadioButton.getValue())
							|| (srcAndDstRadioButton.getValue()))
						query += "<Query srcip=\"" + ip1 + "~" + ip2 + "\"/>";
					if ((asDstRadioButton.getValue())
							|| (srcAndDstRadioButton.getValue()))
						query += "<Query dstip=\"" + ip1 + "~" + ip2 + "\"/>";
				} else {
					// invalid ip
				}
			}
		} else {
			query += "<Query ";
			String val = dstip1TextBox.getText().trim();
			if (val.length() > 0) {
				// System.out.println("Val: '" + val + "'");
				String pred = dstipPredComboBox.getItemText(dstipPredComboBox
						.getSelectedIndex());
				String val2 = dstip2TextBox.getText().trim();
				if ((val2.length() > 0) && (pred.equals("between"))) {
					query += "dstip=\"" + val + "~" + val2 + "\" ";
				} else if (!pred.equals("between")) {
					query += "dstip=\"" + pred + val + "\" ";
				}
			}
			val = srcip1TextBox.getText().trim();
			if (val.length() > 0) {
				String pred = srcipPredComboBox.getItemText(srcipPredComboBox
						.getSelectedIndex());
				String val2 = srcip2TextBox.getText().trim();
				if ((val2.length() > 0) && (pred.equals("between"))) {
					query += "srcip=\"" + val + "~" + val2 + "\" ";
				} else if (!pred.equals("between")) {
					query += "srcip=\"" + pred + val + "\" ";
				}

			}
			val = dstport1TextBox.getText().trim();
			if (val.length() > 0) {
				String pred = dstportPredComboBox
						.getItemText(dstportPredComboBox.getSelectedIndex());
				String val2 = dstport2TextBox.getText().trim();
				if ((val2.length() > 0) && (pred.equals("between"))) {
					if (Integer.parseInt(val) > Integer.parseInt(val2)) {
						// alert("Invalid port range. Lower bound (" + val + ")
						// greater than upper bound (" + val2 + ").");
					}
					query += "dstport=\"" + val + "~" + val2 + "\" ";
				} else if (!pred.equals("between")) {
					query += "dstport=\"" + pred + val + "\" ";
				}
			}
			query += "/>";

		}

		Date startDate = startDatePicker.getValue();
		Date endDate = endDatePicker.getValue();

		query += "<Timestamp start=\"" + startDate.getTime() + "\"";
		if (endDate != null) {
			query += " end=\"" + endDate.getTime() + "\"";
		}
		query += "/>";
		query += "</Query>";
		System.out.println(query);
		return query;
	}

	private int getLimit() {
		if (unlimitedCheckBox.getValue())
			return -1;

		String temp = this.limitTextBox.getText().trim();

		try {
			int limit = Integer.parseInt(temp);
			return limit;
		} catch (Exception e) {

		}
		return 1000000;
	}

	private String getNote() {
		String temp = this.noteTextArea.getText().trim();
		temp = NetUtil.convertToHex(temp);
		if (temp.length() > 0)
			return temp;
		return "";
	}

	private QueryOptions getQueryOptions() {
		// int[] types = getTypes();
		QueryOptions qo = new QueryOptions();
		qo.note = getNote();

		qo.ipSearchType = GUIUtil.IP_SEARCH_AS_BOTH;
		if (this.asSrcRadioButton.getValue())
			qo.ipSearchType = GUIUtil.IP_SEARCH_AS_SRC;
		else if (this.asDstRadioButton.getValue())
			qo.ipSearchType = GUIUtil.IP_SEARCH_AS_DST;

		qo.queryType = GUIUtil.QUERY_TYPE_IPS;
		if (this.searchCriteriaRadioButton.getValue())
			qo.queryType = GUIUtil.QUERY_TYPE_SEARCH;

		qo.startDate = startDatePicker.getValue();
		qo.endDate = endDatePicker.getValue();
		qo.dstIP1 = this.dstip1TextBox.getText();
		qo.dstIP2 = this.dstip2TextBox.getText();
		qo.dstIPPred = this.dstipPredComboBox.getSelectedIndex() + 1;
		qo.srcIP1 = this.srcip1TextBox.getText();
		qo.srcIP2 = this.srcip2TextBox.getText();
		qo.srcIPPred = this.srcipPredComboBox.getSelectedIndex() + 1;
		if (dstport1TextBox.getText().length() > 0)
			qo.dstPort1 = Integer.parseInt(dstport1TextBox.getText());
		if (dstport2TextBox.getText().length() > 0)
			qo.dstPort2 = Integer.parseInt(dstport2TextBox.getText());
		qo.dstPortPred = this.dstportPredComboBox.getSelectedIndex() + 1;
		qo.ips = this.ips.getText();
		qo.ips.replaceAll("\n", " ");

		for (int i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].getValue())
				qo.setType(types[i], true);
		}

		return qo;
	}

	public int[] getTypes() {
		Vector<Integer> types = new Vector<Integer>();

		for (int i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].getValue())
				types.addElement(this.types[i]);
		}

		int[] temp = new int[types.size()];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = types.elementAt(i);
		}

		return temp;

	}

	public void initWidgets() {
		this.limitTextBox.setStyleName("txtbox");
		this.noteTextArea.setStyleName("txtbox");
		this.ips.setStyleName("txtbox");
		this.srcip1TextBox.setStyleName("txtbox");
		this.srcip2TextBox.setStyleName("txtbox");
		this.dstip1TextBox.setStyleName("txtbox");
		this.dstip2TextBox.setStyleName("txtbox");
		this.dstport1TextBox.setStyleName("txtbox");
		this.dstport2TextBox.setStyleName("txtbox");
		this.dstport2TextBox.setStyleName("txtbox");

		ToolTip tip1 = new ToolTip();
		tip1
				.setHtml("Use this list box to enter a list of space or new line separated IP addresses. Leave of training octets to perform a range search (e.g. 1.2.3 will search IPs between 1.2.3.0 and 1.2.3.255).");
		tip1.applyTo(ips.getElement());

		searchCriteriaTable.getWidget(0, 3).setVisible(false);
		searchCriteriaTable.getWidget(1, 3).setVisible(false);
		searchCriteriaTable.getWidget(2, 3).setVisible(false);
		searchCriteriaTable.getWidget(0, 4).setVisible(false);
		searchCriteriaTable.getWidget(1, 4).setVisible(false);
		searchCriteriaTable.getWidget(2, 4).setVisible(false);

		startDatePicker.getTextBox().setText("");
		// startDateField.setText("");
		endDatePicker.getTextBox().setText("");
		// endDateField.setText("");

		ipsRadioButton.setValue(true);
		srcAndDstRadioButton.setValue(true);

		ips.setText("");
		this.limitTextBox.setText("1000000");
		this.noteTextArea.setText("");
		this.textPayloadRadioButton.setValue(true);

		srcip1TextBox.setText("");
		srcip2TextBox.setText("");
		dstip1TextBox.setText("");
		dstip2TextBox.setText("");
		dstport1TextBox.setText("");
		dstport2TextBox.setText("");
	}

	public void query() {
		QueryServiceAsync NetFSEService = (QueryServiceAsync) GWT
				.create(QueryService.class);

		ServiceDefTarget endpoint = (ServiceDefTarget) NetFSEService;
		endpoint.setServiceEntryPoint(NetFSEClient.QUERY_URL);

		AsyncCallback callback = new AsyncCallback() {
			public void onFailure(Throwable caught) {
				saveQueryOptions(null);
			}

			public void onSuccess(Object result) {
				String resultStr = (String) result;
				String queryID = resultStr;
				saveQueryOptions(queryID);
				client.openQuery(queryID);
			}
		};

		int[] types = getTypes();
		String query = generateQuery();
		// System.out.println(query);
		NetFSEService.query(query, this.getNote(), types, this.getLimit(), -1,
				callback);
	}

	private void saveQueryOptions(String queryID) {

		QueryServiceAsync NetFSEService = (QueryServiceAsync) GWT
				.create(QueryService.class);

		ServiceDefTarget endpoint = (ServiceDefTarget) NetFSEService;
		endpoint.setServiceEntryPoint(NetFSEClient.QUERY_URL);

		AsyncCallback callback = new AsyncCallback() {
			public void onFailure(Throwable caught) {
				NetFSEClient.redirectToHome();
			}

			public void onSuccess(Object result) {
				client.queryHistory();
			}
		};

		QueryOptions options = getQueryOptions();
		options.queryID = queryID;
		NetFSEService.saveQueryOptions(options, callback);

	}

	public void setSearchCriteria(QueryOptions options) {
		initWidgets();

		this.noteTextArea.setText(options.note);
		if (options.limit != -1)
			this.limitTextBox.setText(options.limit + "");
		else
			this.unlimitedCheckBox.setValue(true);
		// this.appendTextBox.setText(options.appendQueryID);
		// this.payloadTextBox.setText(options.payload);

		// if (options.payloadType == GUIUtil.PAYLOAD_TYPE_TEXT)
		// this.textPayloadRadioButton.setChecked(true);
		// if (options.payloadType ==
		// GUIUtil.PAYLOAD_TYPE_REGEX)
		// this.regExPayloadRadioButton.setChecked(true);
		// else if (options.payloadType ==
		// GUIUtil.PAYLOAD_TYPE_HEX)
		// this.hexPayloadRadioButton.setChecked(true);

		if (options.queryType == GUIUtil.QUERY_TYPE_IPS)
			this.ipsRadioButton.setValue(true);
		else if (options.queryType == GUIUtil.QUERY_TYPE_SEARCH)
			this.searchCriteriaRadioButton.setValue(true);
		// else if (options.queryType ==
		// GUIUtil.QUERY_TYPE_PAYLOAD)
		// this.payloadRadioButton.setChecked(true);

		if (options.ipSearchType == GUIUtil.IP_SEARCH_AS_BOTH)
			this.srcAndDstRadioButton.setValue(true);
		else if (options.ipSearchType == GUIUtil.IP_SEARCH_AS_DST)
			this.asDstRadioButton.setValue(true);
		else if (options.ipSearchType == GUIUtil.IP_SEARCH_AS_SRC)
			this.asSrcRadioButton.setValue(true);

		this.startDatePicker.setValue(options.startDate);
		this.endDatePicker.setValue(options.endDate);
		// this.startDateField.setText(options.startTS);
		// this.endDateField.setText(options.endTS);

		this.dstip1TextBox.setText(options.dstIP1);
		this.dstip2TextBox.setText(options.dstIP2);
		if (options.dstIP2.length() > 0) {
			dstip2TextBox.setVisible(true);
		}
		this.dstipPredComboBox.setSelectedIndex(options.dstIPPred - 1);

		this.srcip1TextBox.setText(options.srcIP1);
		this.srcip2TextBox.setText(options.srcIP2);
		if (options.srcIP2.length() > 0) {
			srcip2TextBox.setVisible(true);
		}
		this.srcipPredComboBox.setSelectedIndex(options.srcIPPred - 1);

		if (options.dstPort1 != -1) {
			this.dstport1TextBox.setText(options.dstPort1 + "");
		}
		if (options.dstPort2 != -1) {
			this.dstport2TextBox.setText(options.dstPort2 + "");
		}
		this.dstportPredComboBox.setSelectedIndex(options.dstPortPred - 1);

		this.ips.setText(options.ips);

		for (int i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i].setValue(options.isSelected(types[i]));
		}

	}

	public void setTypes(TypeInfo typeInfo) {
		int numTypes = typeInfo.getNumTypes();
		this.checkBoxes = new CheckBox[numTypes];
		this.types = new int[numTypes];
		this.typeIdxs = new int[numTypes];
		this.typeNames = new String[numTypes];
		int maxType = -1;
		int[] types = typeInfo.getTypes();
		for (int i = 0; i < numTypes; i++) {
			checkBoxes[i] = new CheckBox(typeInfo.getTypeName(types[i]));
			checkBoxTable.setWidget(i, 0, checkBoxes[i]);
			if (maxType < types[i])
				maxType = types[i];
		}

		this.typeIdxs = new int[maxType + 1];
		for (int i = 0; i < numTypes; i++) {
			final int type = types[i];
			typeIdxs[type] = i;

			this.types[i] = type;
			this.typeNames[i] = typeInfo.getTypeName(types[i]);
		}

	}
}
